﻿using System.IO;
using System.Reflection;
using System.Text;

namespace MyProject.Core
{
    public class PathHelper
    {
        public static string ContentRootPath
        {
            get
            {
                string rootDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                if (rootDirectory.Contains("bin"))
                {
                    rootDirectory = rootDirectory[..rootDirectory.IndexOf("bin")];
                }
                return rootDirectory;
            }
        }

        public static string AppConfigPath
        {
            get
            {
                return ContentRootPath + '/' + "config.json";
            }
        }

        // Создание файла с кодом приложения
        public static void GenerateFileAppCode()
        {
            string rootPath = @"" + PathHelper.ContentRootPath;
            var header = "***********************************" + Environment.NewLine;

            var files = Directory.GetFiles(rootPath, "*.cs", SearchOption.AllDirectories);

            var result = files.Select(path => new { Name = Path.GetFileName(path), Contents = File.ReadAllText(path) })
                              .Select(info =>
                                  header
                                + "Filename: " + info.Name + Environment.NewLine
                                + header
                                + info.Contents);


            var singleStr = string.Join(Environment.NewLine, result);
            
            File.WriteAllText(@$"{rootPath}\output-C#.txt", singleStr, Encoding.UTF8);



            files = Directory.GetFiles(rootPath, "*.xaml", SearchOption.AllDirectories);

            var resultXaml = files.Select(path => new { Name = Path.GetFileName(path), Contents = File.ReadAllText(path) })
                              .Select(info =>
                                  header
                                + "Filename: " + info.Name + Environment.NewLine
                                + header
                                + info.Contents);


            singleStr = string.Join(Environment.NewLine, resultXaml);
            
            File.WriteAllText(@$"{rootPath}\output-xaml.txt", singleStr, Encoding.UTF8);

        }
    }
}
